package com.testpro.DesignPattern.FactoryPattern;

import java.util.HashMap;

/**
 * 带缓存的工厂类
 */
public class CacheCarFactory extends AbstractCarFactory {

    private final HashMap<String,Car> Carcache= new HashMap <>();

    @Override
    public Car getnewCar() {
        if (Carcache.get("newCar")!=null)
        {
            return Carcache.get("newCar");
        }else {
            Carcache.put("newCar",new newCar());
            return Carcache.get("newCar");
        }
    }

    @Override
    public Car getOldCar() {
        if (Carcache.get("oldCar")!=null)
        {
            return Carcache.get("oldCar");
        }else {
            Carcache.put("oldCar",new oldCar());
            return Carcache.get("oldCar");
        }
    }

    @Override
    public Car defultInstance() {

        return this.getOldCar();
    }

    @Override
    public Car getInstanceForString(String InstanceName) {

        if (InstanceName.equals("newCar"))
        {
            return this.getnewCar();
        }else if (InstanceName.equals("oldCar"))
        {
            return this.getOldCar();
        }
        else {
            return this.defultInstance();
        }
    }
}
